package org.vectrics.mail.struts;

import java.util.Iterator;
import java.util.List;

import org.apache.ecs.html.A;
import org.apache.ecs.html.Div;
import org.vectrics.common.html.BorderTable;
import org.vectrics.common.html.Cell;
import org.vectrics.common.html.Row;
import org.vectrics.common.html.Table;
import org.vectrics.hibernate.ThreadProperties;
import org.vectrics.mail.FileEmailTemplateCategory;
import org.vectrics.provider.CoreServiceLocator;
import org.vectrics.struts.VectricsTagSupport;


public class TemplateCategoriesMgrTag extends VectricsTagSupport {

	@Override
	protected String makeStartTag() {
		Div mainDiv = new Div();
		
		Table catTable = new BorderTable();
		catTable.setWidth(800);
		
		Row hdrRow = catTable.addRow();
        hdrRow.setDefaultBgImage(this.getContextPath() + "/site-graphics/tbl_hdr.gif");
		hdrRow.setDefaultCellFontClass("tableHdrFont");
		hdrRow.addCell().addText(this.getPhrase("prompt.template.mail.category"));
		hdrRow.addCell().addText(this.getPhrase("prompt.template.mail.description"));
		hdrRow.addCell().addText(this.getPhrase("prompt.domain.name"));
		hdrRow.addCell().addText("&nbsp;");
		
		List<FileEmailTemplateCategory> categories = CoreServiceLocator.getSystemService()
			.findAllEmailTemplateCategories(ThreadProperties.getDomain());
		Iterator<FileEmailTemplateCategory> iter = categories.iterator();
		while (iter.hasNext()) {
			FileEmailTemplateCategory category = iter.next();
			Row row = catTable.addRow();
			row.setDefaultCellFontClass("subFont");
			row.addCell().addText(category.getCode());
			row.addCell().addText(category.getDescription());
			row.addCell().addText(category.getDomain().getDescription());
			
			A editLink = new A();
			editLink.setHref(this.getContextPath() + "/admin/mailmgr/category_edit.do?categoryId=" + category.getId().toString());
			editLink.setClass("subLink");
			editLink.addElement(this.getPhrase("phrase.edit"));
			row.addCell().addElement(editLink);
			
		}
		mainDiv.addElement(catTable);
		return(mainDiv.toString());
	}
	
	

}
